<?php

namespace app\company\model;


use Exception;
use think\Model;

/**
 *  司机提现
 * @property  int $id
 * @property  int $deliver_driver_id 司机ID
 * @property  int $status 状态 1:待审核 2:审核通过 3:审核不通过
 * @property  int $amount 提现金额
 * @property  string $remark 备注
 * @property  string $no
 * @property  int $audit_time 审核时间
 */
class DeliverDriverWithdraw extends Base
{


    /**
     * @param DeliverDriverWithdraw $model
     * @return void
     * @throws Exception
     */
    public static function onBeforeUpdate(DeliverDriverWithdraw $model): void
    {
        if ($status = request()->param('status', 0)) {
            //校验
            if ($status == $model->getOrigin('status')) exception("订单已处理");
            //审核通过
            if ($model->status == 2 && $model->getOrigin('status') != 1) exception("订单状态异常,无法审核");
            //审核拒绝
            if ($model->status == 3 && $model->getOrigin('status') != 1) exception("订单状态异常,无法审核");

            $model->audit_time = time();
            if ($status == 3) {
                //删除提现记录
                DeliverDriverAccountLog::where('withdraw_no', $model->no)->find()->delete();
                //退还金额
                DeliverDriver::where('id', $model->deliver_driver_id)->inc('money', $model->amount)->update();
            }
        }
    }
}